
package com.andy.note.main.baidu.ocr.demo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.andy.note.main.baidu.ocr.demo.utils.Base64Util;
import com.andy.note.main.baidu.ocr.demo.utils.FileUtil;
import com.andy.note.main.baidu.ocr.demo.utils.HttpUtil;

import cn.hutool.json.JSONUtil;

/**
 * 通用文字识别
 */
public class GeneralBasic {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String generalBasic() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
        try {
            // 本地文件路径
            String filePath = "e:/desktop/1.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();
            //            String accessToken = "24.b5c51cdc887425c86ee9b655ed0cdc38.2592000.1625298693.282335-24283341";

            String result = HttpUtil.post(url, accessToken, param);
            //            System.out.println(result);
            // {"words_result":[{"words":"父母爻应吉的条件:"},{"words":"父母爻为喜用种时旺相无伤且不空"},{"words":"2父母为忌仇种时体因无气受制重"},{"words":"3父母爻临忌支旬空"},{"words":"4父母爻临忌支虽然旺相千伤且不空"},{"words":"但世爻(用神空(能避克"},{"words":"父母处以上态为应害体脆集的一面"},{"words":"5此外虽然父母交临喜支相但是由合上的原因"},{"words":"父母爻所处位置非但不最于世爻{用种"},{"words":"反而起到了辅助忌文的作用时此父母爻若旬空"},{"words":"则体现吉类象不空则体现函类象"},{"words":"断卦一定要看具体租合不能用定的断语生搬硬套。"}],"log_id":1401735431103447040,"words_result_num":12}
            Map bean = JSONUtil.toBean(result, Map.class);
            List<Map> list = (List)bean.get("words_result");
            StringBuffer sb = new StringBuffer();
            for (Map map : list) {
                sb.append(map.get("words") + "\n");
            }
            System.out.println("合并后内容：\n" + sb);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        GeneralBasic.generalBasic();
//        String accessToken = AuthService.getAuth();
//        List<String> path = Arrays.asList("e:/desktop/1.jpg", "e:/desktop/2.jpg");
//        moreFile(accessToken, path);
    }

    public static void moreFile(String accessToken, List<String> path) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";

        path.stream().forEach(x -> {
            try {
                byte[] imgData = FileUtil.readFileByBytes(x);
                String imgStr = Base64Util.encode(imgData);
                String imgParam = URLEncoder.encode(imgStr, "UTF-8");
                String param = "image=" + imgParam;
                printResult(HttpUtil.post(url, accessToken, param));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public static void printResult(String result) {
        Map bean = JSONUtil.toBean(result, Map.class);
        List<Map> list = (List)bean.get("words_result");
        StringBuffer sb = new StringBuffer();
        for (Map map : list) {
            sb.append(map.get("words") + "\n");
        }
        System.out.println("合并后内容：\n" + sb);
    }


}